package org.elkastud.model;

import java.security.InvalidParameterException;

/**
 * Class representing single nucleobase.
 * Contains ALPHABET defining valid values of single code.
 * Validation takes place during object construction.
 * This is readonly object - once constructed, there is no way of changing
 * its nucloeobase code.
 *
 * Remarks: in current implementation of Nucleobase.ALPHABET, value of '-' is
 * available for gaps representation, used in DNA Sequence matching
 *
 * @see DNASequence
 * @author Jakub Skierbiszewski
 */
public class Nucleobase
{
	/**
	 * Defines available nucleobase codes. Used for validation
	 * Remarks: '-' is available for gaps representation
	 */
	public static final String ALPHABET = "AGCT-";

	/** 
	 * Nucleobase code. This field is validated with Nucleobase.ALPHABET 
	 * each time object is being constructed.
	 */
	private char mNucleobaseCode;

	/**
	 * Constructs nucleotide from character representation.
	 * Checks passed char validity with Nucleobase.ALPHABET
	 * @param nucleobaseCode
	 * @throws InvalidParameterException when nucleobaseCode is not valid
	 */
	public Nucleobase(char nucleobaseCode) throws InvalidParameterException
	{
		//Validate with Nucleobase.ALPHABET
		if(!validate(nucleobaseCode))
			throw new InvalidParameterException("Nucleobase code ("+nucleobaseCode
					+") is not valid. Use values defined in Nucleobase.ALPHABET "
					+ "(current: " + ALPHABET + ")");
		
		this.mNucleobaseCode = nucleobaseCode;
	}

	/**
	 * Returned nucleobase code is one of characters defined in Nucleobase.ALPHABET
	 * @return nucleobase code
	 */
	public char getNucleobaseCode()
	{
		return mNucleobaseCode;
	}

	/**
	 * Validates each nucleobase code with Nucleobase.ALPHABET
	 * @param codeToValidate
	 * @return	true - code is valid (contained in Nucleobase.ALPHABET)
	 *			false - code is not valid
	 */
	private boolean validate(char codeToValidate)
	{
		return ALPHABET.indexOf(codeToValidate)!=-1;
	}
}
